const Router=require('koa-router');
const { user } = require('./database');
const users=new Router(); 
const sqldata = require('./sqldata');

//获取关注数量
users.get('/',async(ctx)=>{
    let sql;
    let mydata1;
    let mydata2;
    let mydata=[];
    let val=ctx.request.query;
    mydata1 = await new Promise((resolve, reject)=>{
        sql=`select count(*) as adsum from attented where Uid='${val.Uid}'`;
        return sqldata(sql, (err, data)=>{
            if(err) reject(err);
            resolve(data);	// 返回拿到的数据
        })
    })
    mydata2 = await new Promise((resolve, reject)=>{
        sql=`select count(*) as atsum from attention where Uid='${val.Uid}'`;
        return sqldata(sql, (err, data)=>{
            if(err) reject(err);
            resolve(data);	// 返回拿到的数据
        })
    })
    mydata.push(mydata1);
    mydata.push(mydata2);
    ctx.body=mydata;

})

//账户与安全界面
users.get('/my-home/my-account',async(ctx)=>{
    let val=ctx.request.query;
    let mydata = await new Promise((resolve, reject)=>{
        let sql=`select * from user where Uid='${val.Uid}'`;
        return sqldata(sql, (err, data)=>{
            if(err) reject(err);
            resolve(data);	// 返回拿到的数据
        })
    })
    ctx.body = mydata; 
})
users.post('/my-home/my-account',async(ctx)=>{
    let mydata = await new Promise((resolve, reject)=>{
        let sql=`INSERT INTO user VALUES ('${ctx.request.body.Uid}', '${ctx.request.body.Uname}','${ctx.request.body.Usex}','${ctx.request.body.Uday}',
        '${ctx.request.body.Uimage}', '${ctx.request.body.Uphone}', '${ctx.request.body.Uemail}', '${ctx.request.body.Pid}', '${ctx.request.body.Uarea}',
        '${ctx.request.body.Ulevel}') on duplicate key update Uid='${ctx.request.body.Uid}', Uname='${ctx.request.body.Uname}',Usex='${ctx.request.body.Usex}',
        Uday='${ctx.request.body.Uday}', Uimage='${ctx.request.body.Uimage}', Uphone='${ctx.request.body.Uphone}',Uemail= '${ctx.request.body.Uemail}', 
        Pid='${ctx.request.body.Pid}',Uarea= '${ctx.request.body.Uarea}', Ulevel='${ctx.request.body.Ulevel}'`
        return sqldata(sql, (err, data)=>{
            if(err) reject(err);
            resolve(data);	// 返回拿到的数据
        })
    })
    ctx.body = mydata; 
})
//喜欢页面
users.get('/my-like',async(ctx)=>{
    let mydata = await new Promise((resolve, reject)=>{
        let sql=`select * from user,works,workfabulous where workfabulous.Uid='${ctx.request.query.Uid}' and works.Wid=workfabulous.Wid and works.Uid=user.Uid`
        return sqldata(sql, (err, data)=>{
            if(err) reject(err);
            resolve(data);	// 返回拿到的数据
        })
    })
    ctx.body = mydata;
})
users.delete('/my-like',async(ctx)=>{
    let mydata = await new Promise((resolve, reject)=>{
        let sql=`delete from workfabulous where Uid='${ctx.request.body.Uid}' and Wid='${ctx.request.body.Wid}'`
        return sqldata(sql, (err, data)=>{
            if(err) reject(err);
            resolve(data);	// 返回拿到的数据
        })
    })
    ctx.body = mydata;
})
//宠物卡片填写界面
users.post('/my-pet-publish',async(ctx)=>{
    let val=ctx.request.body;
    let sql;
    console.log(val)
    if(val.Pid=='undefined'){
        let mydata = await new Promise((resolve, reject)=>{
            sql=`insert into pet (Pname,Psex,Page,Pweight,Pvarie,Photo,Pinject,Pintroduction) values ('${val.Pname}',
            '${val.Psex}',${val.Page},'${val.Pweight}','${val.Pvarie}',
            '${val.Photo}','${val.Pinject}','${val.Pintroduction}')`
            return sqldata(sql, (err, data)=>{
                if(err) reject(err);
                resolve(data);	// 返回拿到的数据
            })
        })
        let mydata1 = await new Promise((resolve, reject)=>{
            sql=`select Pid from pet where Pname='${val.Pname}' and Psex='${val.Psex}' and Page=${val.Page} and Pweight='${val.Pweight}' 
            and Pvarie='${val.Pvarie}' and Photo='${val.Photo}' and Pinject='${val.Pinject}' and Pintroduction='${val.Pintroduction}'`
            return sqldata(sql, (err, data)=>{
                if(err) reject(err);
                resolve(data);	// 返回拿到的数据
            })
        })
        console.log(mydata1)
        let mydata2 = await new Promise((resolve, reject)=>{
            sql=`update user set Pid='${mydata1[0].Pid}' where Uid='${val.Uid}'`
            return sqldata(sql, (err, data)=>{
                if(err) reject(err);
                resolve(data);	// 返回拿到的数据
            })
        })
    }else{
        let mydata3 = await new Promise((resolve, reject)=>{
            sql=`update pet set Pname='${val.Pname}',Psex='${val.Psex}',Page='${val.Page}',Pweight='${val.Pweight}',Pvarie='${val.Pvarie}',
            Photo='${val.Photo}',Pinject='${val.Pinject}',Pintroduction='${val.Pintroduction}' where Pid='${val.Pid}'`
            return sqldata(sql, (err, data)=>{
                if(err) reject(err);
                resolve(data);	// 返回拿到的数据
            })
        })
    }
    
    
})
//宠物卡片
users.get('/my-pet-publish',async(ctx)=>{
    let mydata = await new Promise((resolve, reject)=>{
        let sql=`select * from pet where Pid in (select Pid from user where Uid='${ctx.request.query.Uid}')`
        return sqldata(sql, (err, data)=>{
            if(err) reject(err);
            resolve(data);	// 返回拿到的数据
        })
    })
    console.log(mydata)
    ctx.body = mydata;
})
users.get('/my-petTrip',async(ctx)=>{
    ctx.body='宠物行程'
})
users.get('/my-secret',async(ctx)=>{
    ctx.body='隐私页面'
})
//达人认证
users.get('/my-home/my-talent',async(ctx)=>{
    let mydata = await new Promise((resolve, reject)=>{
        let sql=`select Ulevel from user where Uid='${ctx.request.query.Uid}'`
        return sqldata(sql, (err, data)=>{
            if(err) reject(err);
            resolve(data);	// 返回拿到的数据
        })
    })
    ctx.body = mydata;
})
//作品页面
users.get('/my-works',async(ctx)=>{
    let mydata = await new Promise((resolve, reject)=>{
        let sql=`select Wid,Wtitle,Wzan,Wimage from works where Uid='${ctx.request.query.Uid}'`
        return sqldata(sql, (err, data)=>{
            if(err) reject(err);
            resolve(data);	// 返回拿到的数据
        })
    })
    ctx.body = mydata;
})
users.delete('/my-works',async(ctx)=>{
    let mydata = await new Promise((resolve, reject)=>{
        let sql=`delete from works where Uid='${ctx.request.body.Uid}' and Wid='${ctx.request.body.Wid}'`
        return sqldata(sql, (err, data)=>{
            if(err) reject(err);
            resolve(data);	// 返回拿到的数据
        })
    })
    ctx.body = mydata;
})
module.exports=users;